package com.autologistics.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.autologistics.model.Goods;
import com.autologistics.model.Goodskind;
import com.autologistics.model.User;
import com.autologistics.service.GoodsService;
import com.autologistics.service.GoodsServiceImpl;
import com.autologistics.service.GoodskindService;
import com.autologistics.service.UserService;
import com.autologistics.util.GobalValue;
import com.autologistics.util.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class GoodAction extends ActionSupport implements ModelDriven<Goods>{
	
	
	
	private static final long serialVersionUID = 1L;
	private GoodsService goodsService;
	private GoodskindService goodskindService;
	
	private List<Goods> goodsList;
	private List<Goodskind> goodsKindList;
	private int userId;
	private String password;
	private User user=new User();
	Goods goods=new Goods();
	private PageBean pageBean;
	//默认是1
	private int page=1;
	
	private double price;
	private String message;
	
	
	
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public GoodskindService getGoodskindService() {
		return goodskindService;
	}

	public void setGoodskindService(GoodskindService goodskindService) {
		this.goodskindService = goodskindService;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public List<Goods> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<Goods> goodsList) {
		this.goodsList = goodsList;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}
	

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	
		
	public List<Goodskind> getGoodsKindList() {
		return goodsKindList;
	}

	public void setGoodsKindList(List<Goodskind> goodsKindList) {
		this.goodsKindList = goodsKindList;
	}


	@Override
	public Goods getModel() {
		// TODO Auto-generated method stub
		return this.goods;
	}
	
	
	/**
	 * to add a new Good
	 * @throws Exception 
	 */
	public String addGood() throws Exception{
		System.out.println("line 164::::::addGood");
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		if(GobalValue.FAIL.equals(hasSession(session))){
			return GobalValue.FAIL;
		}
		int userid=(int) session.getAttribute("id");
		user.setId(userid);
//		System.out.println("addGood    addGood   addGood   addGood");
//		System.out.println("addGood");
//		if(getPrice()>=0.0&&getPrice()<99999.99){
			goods.setUser(user);
			this.goodsService.addGood(goods);
			
			return this.page();
//		}else{
//			System.out.println("else else else ");
//			message="请输入正确的价格！";
//			return "fail";		
	}
	
	/**
	 * to control jump into addGood.jsp.
	 * @return
	 */
	public String preAddGood(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		if(GobalValue.FAIL.equals(hasSession(session))){
			return GobalValue.FAIL;
		}
		goodsKindList=goodskindService.listAll();
		return SUCCESS;
	}

	
	/**
	 * prepare to update good info. 
	 * SO need to get good info to display on screen.
	 * @return
	 */
	public String preUpdateGood(){
//		Map<String,Object> attributes=ActionContext.getContext().getSession();
		//to get the id of special good.
//		int id=Integer.parseInt(attributes.get("id").toString());
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		if(GobalValue.FAIL.equals(hasSession(session))){
			return GobalValue.FAIL;
		}
		int goodsId=goods.getId();	
		System.out.println("preUpdateGoods:	goodsId:  "+goodsId);
		goods=goodsService.getGoodById(goodsId);
		goodsKindList=goodskindService.listAll();
		
		if(goods==null){
			return ERROR;
		}else{
			return SUCCESS;
		}
	}
	/**
	 * to update object good.
	 */
	public String updateGood(){
		HttpServletRequest request=ServletActionContext.getRequest();
		
		//to get session user id. 
		HttpSession session=request.getSession();
		if(GobalValue.FAIL.equals(hasSession(session))){
			return GobalValue.FAIL;
		}
		int userid=(int) session.getAttribute("id");
		
		user.setId(userid);
		goods.setUser(user);
		
		goodsService.updateGood(goods);
		
		return this.goodsList();
	}
	
	/**
	 * after delete good ;display all goods.
	 * @return
	 */
	public String deleteGood(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		if(GobalValue.FAIL.equals(hasSession(session))){
			return GobalValue.FAIL;
		}
		int userid=(int) session.getAttribute("id");
		user.setId(userid);
		goods.setUser(user);
		
		int id=goods.getId();
		goodsService.deleteGood(id);
		return this.goodsList();
	}
	
	/**
	 * first step:to get userId
	 * second:to get list of goods in special userId.
	 * @return
	 */
	public String goodsList() {
		//得到session
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		if(GobalValue.FAIL.equals(hasSession(session))){
			return GobalValue.FAIL;
		}
		int userid=(int) session.getAttribute("id");
		System.out.println("session :user:id"+userid);
		//暂时将userId定死，做测试 
//		int tempuserId=1;
		
		goodsList=goodsService.listAll(userid);
		
		//to get list of goodskind.
		System.out.println("to invoke goodskindService");
		goodsKindList=goodskindService.listAll();
		
		return SUCCESS;
	}
	
	public String searchGoods() {
		//暂时将userId定死，做测试 
//		int userId=goods.getUser().getId();
//		int tempuserId=1;
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		if(GobalValue.FAIL.equals(hasSession(session))){
			return GobalValue.FAIL;
		}
		int userid=(int) session.getAttribute("id");
		String name=goods.getName();
//		goodsList=goodsService.listAll(tempuserId);
		goodsList=goodsService.searchGoods(userid,name);
		//to get list of goodskind.
		System.out.println("to invoke goodskindService");
		goodsKindList=goodskindService.listAll();
		
		return SUCCESS;
	}

	//分页功能
    public String page()throws Exception{  
		//得到session
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		if(GobalValue.FAIL.equals(hasSession(session))){
			return GobalValue.FAIL;
		}
//		int userid=(int) session.getAttribute("id");
//		int userid=1;
//		System.out.println("session :user:id"+userid);
		//page是从jsp中得到
//		page=request.getAttribute("");
		int userid=(int) session.getAttribute("id");
		System.out.println("page:"+page);
	    this.pageBean = (PageBean) goodsService.queryForOnePage(GobalValue.pageSize, page,userid);
	    //获取封装了分页信息和数据的pageBean  
	    this.goodsList = this.pageBean.getGoodsList(); //获取数据
	    for (Goods g:goodsList){
	    	g.setGoodskind(goodskindService.getById(g.getGoodskind().getId()));
	    	}
	    goodsKindList=goodskindService.listAll();
	    return SUCCESS;  
    }

/*	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		if(session==null){
			return "fail";
		}else{
			
		}
	}*/
    
    /**
     * 判断是否有session
     * @param session
     * @return
     */
    public String hasSession(HttpSession session){
    	if(session==null){
    		return GobalValue.FAIL;
    	}else{
    		return SUCCESS;
    	}
    }
    
    
	

}
